package com.bjwl.wybx.controller.safty;

import com.bjwl.wybx.VO.UserLoginVO;
import com.bjwl.wybx.exception.LoginException;
import com.bjwl.wybx.exception.SysException;
import com.bjwl.wybx.service.UserService;
import com.bjwl.wybx.util.Constants;
import com.bjwl.wybx.util.CurrUser;
import com.bjwl.wybx.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/safty/login")
public class LoginController {
    @Autowired
    private UserService userService;
    @PostMapping("/userLogin")
    public Result login(@RequestBody UserLoginVO userLoginVO, HttpSession session){
        System.out.println("userLoginVO:"+userLoginVO);
        CurrUser currUser = null;
        try {
            currUser = userService.login(userLoginVO);
            //将currUser放入session中 //使用常量
            session.setAttribute(Constants.SESSION_ATTR_CURRUSER,currUser);
            //
            System.out.println("登录:"+session.getId());
            return Result.success("登录成功！",currUser);
        }  catch (SysException e) {
            e.printStackTrace();
            return Result.fail("系统升级中");
        }catch (LoginException e){
            return Result.fail(e.getMessage());
        }
    }
}
